package com.codes.controller;

import com.codes.core.common.enumtype.WorkOrderState;
import com.codes.core.common.result.Result;
import com.codes.core.utils.excel.ExcelUtil;
import com.codes.service.OrderInfoSV;
import com.codes.service.OrderInfoWMSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Date 2020/5/29 18:39
 * @Created by peanut
 */

@RestController
@Api(tags = " 周 月 计划 接口")
@RequestMapping("/order-info-w-m")
public class OrderInfoWMCtr {


    @Autowired
    private OrderInfoWMSV orderInfoWMSV;


    @ApiOperation("详情分页")
    @GetMapping("/page")
    public Result page(@RequestParam( defaultValue="1",required=true) int   page,
                       @RequestParam( defaultValue="10",required=true) Integer limit,
                       @RequestParam Map<String,Object> map){

        PageHelper.startPage(page,limit);
        // Map<String, Object> map =  new HashMap<>();
        List<Map<String, Object>> users = orderInfoWMSV.getListToPageExport(map);
        PageInfo<Map<String, Object>> info = new PageInfo< >(users);
        return Result.pages(info.getList(),(int)info.getTotal());
    }

    @ApiOperation("批量删除周 月计划")
    @PostMapping("/delete-batch")
    public Result deleteBatch(@RequestParam Map<String,Object> map){
        String ids = MapUtils.getString(map,"ids");
        int re  =0;
        if(StringUtils.isNotBlank(ids)){
            String[] strs = ids.split(",");
            List<String> idss = Arrays.asList(strs) ;
            re = orderInfoWMSV.deleteBatch(idss);
            return Result.success("删除成功");
        }
        return  Result.failed("删除失败");
    }

    @ApiOperation("导出工单周 月计划")
    @PostMapping("/export-wm")
    public void exportList(  HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        String  orderType = request.getParameter("orderType");
        String  startDate = request.getParameter("startDate");
        String  endDate = request.getParameter("endDate");
        String  projectId = request.getParameter("projectId");
        map.put("orderType",orderType);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("projectId",projectId);
        String  execlName = null;
        if(1 == Integer.parseInt(orderType)){
            execlName = "周计划工单-";
        }else {
            execlName = "月计划工单-";
        }
        // 表头
        Object[] titles = new Object[]{"序号","项目名称","楼栋","楼层","构件名称","车次","要货开始日期","要货结束日期"};

        List<Object[]> data = new ArrayList<>();
        List< Map<String, Object>> list = orderInfoWMSV.getListToPageExport(map);
        Integer index =1;
        for ( Map<String, Object> m :list   ) {

            Object[] obj = new Object[]{

                    index,
                    MapUtils.getString(m,"projectName"),
                    MapUtils.getString(m,"buildingName"),
                    MapUtils.getString(m,"floors"),
                    MapUtils.getString(m,"productName"),
                    MapUtils.getString(m,"trainNo"),
                    MapUtils.getString(m,"startDate"),
                    MapUtils.getString(m,"endDate"),

            };
            data.add(obj);
            index++;
        }
        ExcelUtil.export(request,response,execlName + DateFormatUtils.format(new Date(), "yyyyMMdd"),titles, data);
    }



}